<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortletScan.php 455 2011-01-07 22:53:55Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalPortletRecord');

class PortletScan extends PortalAdminPage
{

	public function  onInit($p)
	{
		// Create the Portles admin directory
		$padmDir	= Prado::getPathOfNamespace('Application.Pages.Admin.Portlets');
		if(!file_exists($padmDir))
		{
			$this->reportError(0, 'Please create the Admin/Portles directory, and make it writable by the Web server');
		}

		parent::onInit($p);
	}
	public function dataBind()
	{
		//$cdb		= $this->Application->DbConnection;
		$errors		= array();
		$newpages	= array();
		//$res		= Null;

		// Scan the portlets dir
		$pagesdir   = str_replace("\\", "/",$this->Application->BasePath . "/Portlets/");
		$dirs		= $this->getDirsInPath($pagesdir);

		if(is_array($dirs) && count($dirs) != 0)
		{
			foreach($dirs as $dir)
			{
				$pname = str_replace($this->Application->BasePath . "/Portlets/","", $dir);
				$pRec = PortalPortletRecord::finder()->findByname($pname);
				
				if(is_null($pRec))
				{
					$installfile = $this->Application->BasePath . "/Portlets/" . $dir . '/Install/' . $pname . '.php';
					if(file_exists($installfile))
					{
						Prado::using('Application.Portlets.' . $pname . '.Install.' . $pname);

						$classname	= $pname;
						if(class_exists($classname,false))
						{
							$portlet = new $classname;

							if($portlet->PortletInstall())
							{
								$pRec				= new PortalPortletRecord();
								$pRec->name			= $pname;
								$pRec->displayname	= $pname;
								$pRec->active		= true;
								$pRec->hasadmin		= $portlet->HasAdminFunc;
								$pRec->save();
								$newpages[]['name'] = $pname;
							}
						}
						else
						{
							$this->reportError(0,Prado::localize('Portlet installation file {0} does not contain class {1}',array($classfile,$classname)));
						}
					}
					else
						PortalUtil::Info ($installfile);
				}
			}
		}

		if(count($newpages) > 0)
		{
			$this->Repeater->Visible	= true;
			$this->Repeater->Enabled	= true;
			$this->Repeater->DataSource		= $newpages;
			$this->Repeater->dataBind();
		}
		else
		{
			$this->Repeater->Visible	= false;
			$this->Repeater->Enabled	= false;
			$this->pnlNoNewPortlets->Visible	= true;
			$this->pnlNoNewPortlets->Enabled	= true;
		}

		if(count($errors) != 0)
		{
			$this->ErrRepeater->Visible = true;
			$this->ErrRepeater->Enabled = true;
			$this->ErrRepeater->DataSource = $errors;
			$this->ErrRepeater->dataBind();
		}
	}

	public function getDirsInPath($path)
	{
		$dirs	= array();
		$folder = opendir($path);

		while (false !== ($dir = readdir($folder)))
		{
			if(is_dir($path . '/' . $dir))
			{
				if($dir !== '.' && $dir !== '..' && $dir !== '.svn')
					$dirs[] = $dir;
			}
		}
		closedir($folder);
		return $dirs;
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Home');

		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function GetCssStyle($item)
	{
		switch($item->priority)
		{
			case 0:	return 'background-color:#FFFF80';break;
			case 1:	return 'background-color:#80FF80';break;
			case 2:	return 'background-color:#FF8080';break;
		}
	}
}

function rscandir($base='', &$data=array())
{
	$array = array_diff(scandir($base), array('.', '..', '.svn','System','Admin','Install')); # remove ' and .. from the array */

	foreach($array as $value) /* loop through the array at the level of the supplied $base */
	{
    	if (is_dir(str_replace("\\", "/", $base.$value)))  /* if this is a directory */
    	{
			$data[] = str_replace("\\", "/",$base.$value).'/'; /* add it to the $data array */
			$data = rscandir($base.$value.'/', $data); /* then make a recursive call with the current $value as the $base supplying the $data array to carry into the recursion */
		}
		elseif (is_file(str_replace("\\","/",$base.$value))) /* else if the current $value is a file */
		{
			$data[] = str_replace("\\", "/",$base.$value); /* just add the current $value to the $data array */
		}
	}

	return $data; // return the $data array
}
?>